#!/usr/bin/env python
#
# Copyright 2009 Liu, Xiaokang <4341322@gmail.com>
# Blog <http://www.ioio.name/>
#

import time
import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.api import urlfetch
from google.appengine.api import mail

class MainPage(webapp.RequestHandler):
  def get(self):
    self.response.out.write('Are Your Site is uptime now?')

class WebCheck(webapp.RequestHandler):
  def get(self):
    url = 'http://www.chinaventure.com.cn/'
    result = urlfetch.fetch(url)
    if result.status_code == 200:
      #self.response.out.write(result.content)
      self.response.out.write('Site Online Now:%s' % time.ctime())
    else:
      SendNoticeMail(result.status_code)
      self.response.out.write('Site Offline Now:%s' % time.ctime())

def SendNoticeMail(status):
  message = mail.EmailMessage(sender='guoyao@gmail.com',
                              subject='Site Offline Notice')
  message.to = '15801570891@139.com'
  message.body = """Hello Darcy:
Your site chinaventure is offline now.Please check it.
The status code is %s ,%s.""" % (status, time.ctime())
  message.send()
      
application = webapp.WSGIApplication([
  ('/', MainPage),
  ('/tasks/webmonitor', WebCheck)
], debug=True)

def main():
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
  main()
